package com.pan.service.impl;

import com.pan.dao.OperationStockHistoryDao;
import com.pan.dao.OperationStockHistorySelectDao;
import com.pan.entity.OperationStockHistory;
import com.pan.entity.Result;
import com.pan.enume.ResultEnum;
import com.pan.service.OperationStockHistoryService;
import com.pan.tools.ConvertObject;
import com.pan.tools.RedisTools;
import com.pan.vo.OperationStockHistoryInsertVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

@Service("operationStockHistoryService")
public class OperationStockHistoryServiceImpl implements OperationStockHistoryService {

    @Resource
    private RedisTools redisTools;

    @Resource
    private OperationStockHistoryDao operationStockHistoryDao;

    @Resource
    private OperationStockHistorySelectDao operationStockHistorySelectDao;

    @Transactional
    @Override
    public Result insert(String adminTicket, OperationStockHistoryInsertVo operationStockHistoryInsertVo){
        OperationStockHistory operationStockHistory = (OperationStockHistory) ConvertObject
                .convertObject(OperationStockHistory.class, operationStockHistoryInsertVo);
        Result<Object> resultId = redisTools.hget("admin:"+adminTicket,"id");
        Long adminId = Long.parseLong((String) resultId.getData());
        Timestamp nowTime = new Timestamp(System.currentTimeMillis());
        operationStockHistory.setAdminId(adminId)
                .setUpdateDate(nowTime);
        Long id = operationStockHistoryDao.insert(operationStockHistory);
        return new Result(ResultEnum.SUCCESS,id);
    }

    @Override
    public Result<List<OperationStockHistory>> selectByStockId(String adminTicket, Long stockId){
        List<OperationStockHistory> li = operationStockHistorySelectDao.selectByStockId(stockId);
        if (li==null||li.size()==0){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        return new Result<>(ResultEnum.SUCCESS,li);
    }

    @Override
    public Result<List<OperationStockHistory>> selectByAdminId(String adminTicket, Long adminId){
        List<OperationStockHistory> li = operationStockHistorySelectDao.selectByAdminId(adminId);
        if (li==null||li.size()==0){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        return new Result<>(ResultEnum.SUCCESS,li);
    }

    @Override
    public Result<List<OperationStockHistory>> selectByUpdateDate(String adminTicket, String y, String m, String d) throws ParseException {
        List<OperationStockHistory> li = operationStockHistoryDao.select();
        String date = y+"-"+m+"-"+d;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long startDate = simpleDateFormat.parse(date).getTime();
        Long endDate = startDate + (60*60*24*1000);
        li = li.stream().filter(i->{
            Long updateDate = i.getUpdateDate().getTime();
            if (updateDate>startDate && updateDate<endDate){
                return true;
            }
            else {
                return false;
            }
        }).collect(Collectors.toList());
        return new Result<>(ResultEnum.SUCCESS,li);
    }



}
